package vn.com.ltv.lo;

import java.util.ArrayList;

import vn.com.ltv.lo.common.ConfigsAndroid;
import vn.com.ltv.lo.model.Food;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.three20.ui.TTImageView;

public class DetailCategoryActivity extends Activity {

	GridView gridView2;
	ArrayList<Food> foods = new ArrayList<Food>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail_category);
		gridView2 = (GridView) findViewById(R.id.gridView2);
		for (int i = 0; i < 50; i++) {
			Food food = new Food();
			food.setImage("http://thongcaobaochi.com.vn/public/media/media/picture/1392280264-dat-thuc-an-truc-tuyen.jpg");
			food.setName("KFC loại " + (i+1));
			food.setPrice(100);
			food.setRate(3.5f);
			foods.add(food);
		}
		
		gridView2.setAdapter(new ListFoodAdapter(DetailCategoryActivity.this, foods));
		
		
	}


	

	public class ListFoodAdapter extends BaseAdapter{

		Context context;
		ArrayList<Food> categories = new ArrayList<Food>();
		LayoutInflater infalInflater;
		FoodHolder catListHolder;
		public ListFoodAdapter(Context context, ArrayList<Food> categories){
			this.context = context;
			this.categories = categories;
			infalInflater = (LayoutInflater) this.context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		@Override
		public int getCount() {
			return categories.size();
		}

		@Override
		public Object getItem(int position) {
			return null;
		}

		@Override
		public long getItemId(int position) {
			return 0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			TextView textView = null;
			TTImageView imageView = null;
			TextView textViewPrice;
			RatingBar ratingBar;
			FrameLayout view;
			if (convertView == null) {
				catListHolder = new FoodHolder();
				convertView = infalInflater.inflate(
						R.layout.item_gridview_food, null);
				textView = (TextView) convertView
						.findViewById(R.id.textview_name);
				imageView = (TTImageView) convertView.findViewById(R.id.image_food);
				ratingBar = (RatingBar) convertView.findViewById(R.id.ratingBar1);
				textViewPrice = (TextView) convertView.findViewById(R.id.textview_price);
				view = (FrameLayout) convertView.findViewById(R.id.view);
				catListHolder.textViewName = textView;
				catListHolder.imageViewBookMark = imageView;
				catListHolder.ratingBar = ratingBar;
				catListHolder.textViewPrice = textViewPrice;
				catListHolder.view = view;
				convertView.setTag(catListHolder);

			} else {
				catListHolder = (FoodHolder) convertView.getTag();
				textView = catListHolder.textViewName;
				imageView = catListHolder.imageViewBookMark;
				ratingBar = catListHolder.ratingBar;
				textViewPrice = catListHolder.textViewPrice;
				view = catListHolder.view;
			}
			
			textView.setText(categories.get(position).getName());
			if (ConfigsAndroid.isNetworkAvailable(DetailCategoryActivity.this)) {
				imageView.setImageURL(categories.get(position).getImage());
			}
			ratingBar.setRating(categories.get(position).getRate());
			view.setOnClickListener(new View.OnClickListener() {
				
				@Override
				public void onClick(View v) {
					//-- do nothing
					
				}
			});
			textViewPrice.setText(categories.get(position).getPrice() +" USD");
			
			
			return convertView;
		}
		
		public class FoodHolder {
			TextView textViewName;
			TTImageView imageViewBookMark;
			TextView textViewPrice;
			RatingBar ratingBar;
			FrameLayout view;
		}
	}
}
